import axios from "axios";
import {ElMessage} from "element-plus";
import {removeToken} from "@/utils/cookie.js";
import router from "@/router/index.js";

const service = axios.create({
    baseURL: '/dev-api',
    timeout: 5000,
})
axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";

service.interceptors.response.use(
    (res) => {
        const code = res.data.code;
        const msg = res.data.msg;
        if (code === 3001) {
            ElMessage.error(msg)
            removeToken()
            router.push('/oj/login')
            return Promise.reject(new Error(msg));
        }else if (code !== 1000) {
            ElMessage.error(msg);
            return Promise.reject(new Error(msg));
        }else {
            return Promise.resolve(res.data);
        }
    },
    (error) => {
        return Promise.reject(error)
    }
);

export default service